package com.nondo.dean.product.service;



import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.product.domain.Product;
import com.nondo.dean.product.domain.ProductRelative;
import com.nondo.dean.product.domain.ProductRelativeExample;
import com.nondo.dean.product.mapper.ProductMapper;
import com.nondo.dean.product.mapper.ProductRelativeMapper;
import com.nondo.dean.product.model.result.RelativeResult;

@Component
public class ProductRelativeService {

    @Autowired
    private ProductRelativeMapper  productRelativeMapper;
    
    @Autowired
    private ProductMapper  productMapper;
    
    /**
     * 插入产品关联
     * @param productRelative {@link ProductRelative}
     * @return
     */
    @Transactional
    public int addProductRelative(ProductRelative productRelative) {
        return productRelativeMapper.insertSelective(productRelative);
    }

    /**
     * 修改产品关联
     * @param productRelative {@link ProductRelative}
     * @return
     */
    @Transactional
    public int updateProductRelative(ProductRelative productRelative) {
        return productRelativeMapper.updateByPrimaryKeySelective(productRelative);
    }

    /**
     * 按照主键ID查询产品关联
     * @param id 主键ID
     * @return
     */
    public ProductRelative selectProductRelative(Integer id) {
        return productRelativeMapper.selectByPrimaryKey(id);
    }

    /**
     * 产品关联分页查询列表
     * @param offset
     * @param limit
     * @param productRelative {@link ProductRelative}
     * @return List<{@link ProductRelative}>
     */
    public List<ProductRelative> getProductRelativeList(int offset, int limit, ProductRelative productRelative) {
        ProductRelativeExample example = new ProductRelativeExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProductRelativeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productRelative);
        return productRelativeMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 产品关联总数统计
     * @param productRelative {@link ProductRelative}
     * @return
     */
    public Long count(ProductRelative productRelative) {
        ProductRelativeExample example = new ProductRelativeExample();
        ProductRelativeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productRelative);
        return productRelativeMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param productRelative {@link ProductRelative}
     */
    void setCriteria(ProductRelativeExample.Criteria criteria, ProductRelative productRelative) {
        if (productRelative != null) {
            if (productRelative.getId() != null)
                criteria.andIdEqualTo(productRelative.getId());
            if (productRelative.getProductId() != null)
                criteria.andProductIdEqualTo(productRelative.getId());
            if (productRelative.getRelativeProductId() != null)
                criteria.andRelativeProductIdEqualTo(productRelative.getRelativeProductId());
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

    /**
     * 根据productId获取产品关联关系
     * @param id 产品主关联ID
     * @return List<{@link ProductRelative}>
     */
    public List<ProductRelative> getRelativeProductListByProductId(Integer id) {
        ProductRelativeExample example = new ProductRelativeExample();
        ProductRelativeExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(id);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ProductRelative> productRelativeList = productRelativeMapper.selectByExample(example);
        return productRelativeList;
    }

    /**
     * 根据productId和relativeId获取产品关联关系
     * @param productId 产品ID
     * @param relativeProductId 关联产品ID
     * @return
     */
    public ProductRelative getRelativeByProductIdAndRelativeId(Integer productId, Integer relativeProductId) {
        ProductRelativeExample example = new ProductRelativeExample();
        ProductRelativeExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(productId);
        criteria.andRelativeProductIdEqualTo(relativeProductId);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ProductRelative> productRelativeList = productRelativeMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(productRelativeList) ? productRelativeList.get(0) : null;
    }

    /**
     * 根据产品关联ID查询出相关联的两个产品信息
     * @param id 关联表主键ID
     * @return {@link RelativeResult}
     */
    public RelativeResult getProductByRelativeId(Integer id) {
        RelativeResult relativeBean = new RelativeResult(); 
        relativeBean.setId(id);
        ProductRelative productRelative = productRelativeMapper.selectByPrimaryKey(id);
        Integer productId = productRelative.getProductId();
        Product product = productMapper.selectByPrimaryKey(productId);
        RelativeResult.ProductBean productBean = RelativeResult.covertToBean(product);
        relativeBean.setProduct(productBean);
        Integer relativeProductId = productRelative.getRelativeProductId();
        Product relativeProduct = productMapper.selectByPrimaryKey(relativeProductId);
        RelativeResult.ProductBean relativeProductBean = RelativeResult.covertToBean(relativeProduct);
        relativeBean.setRelativeProduct(relativeProductBean);
        return relativeBean;
    }

}
